import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
// 获取应用实例
const app = getApp()

Page({
  data: {
    isSearch: false,
    value: '',
    dialogclass: 'dialog-position',
    show: false,
    showSubscribe: false,
    imgUrl: 'https://gitee.com/zhang-zhaoming/imgs/raw/master/yidashop/',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    popTitle: '',
    show: false,
    radio: '1',
    gender: '', // 性别
    height: '', // 身高
    weight: '', // 体重
    bust: '', // 胸围
    waist: '', // 腰围
    hip: '', // 臀围
  },
  toMeasureDetail() {
    if (!this.data.gender) {
      wx.showToast({
        icon: 'error',
        title: '请先选择性别'
      })
      return
    }
    if (!this.data.height) {
      wx.showToast({
        icon: 'error',
        title: '请先选择身高'
      })
      return
    }
    if (!this.data.weight) {
      wx.showToast({
        icon: 'error',
        title: '请先选择体重'
      })
      return
    }
    const params = `radio=${this.data.radio}&height=${this.data.height}&weight=${this.data.weight}&bust=${this.data.bust}&waist=${this.data.waist}&hip=${this.data.hip}`
    wx.navigateTo({
      url: '/pages/view/ai-view/measure-detail/measure-detail?' + params,
    })
  },
  changeUserGender() {
    wx.hideTabBar()
    this.setData({
      show: true,
      popTitle: '更改性别',
      // nickName: this.data.userNickname,
      // radio: String(this.data.userGender)
    })
  },
  onClose() {
    const that = this
    if (this.data.popTitle === '更换绑定手机号') {
      // that.toChangeAvatar()
    } else if (this.data.popTitle === '更改昵称') {
      console.log(that.data.nickName)
      that.toEditUserInfo()
    } else if (this.data.popTitle === '更改性别') {
      console.log(that.data.radio)
      if(that.data.radio === '1') {
        this.setData({
          gender: '男'
        })
      } else {
        this.setData({
          gender: '女'
        })
      }
      // that.toEditUserInfo()
    }
    this.setData({
      show: false
    })
    setTimeout(()=> {
      wx.showTabBar()
    }, 300)
  },
  onClick(event) {
    this.setData({
      radio: event.currentTarget.dataset.name
    });
    this.onClose()
  },
  toEditUserInfo() {
    console.log('编辑用户信息')
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true,
        showSubscribe: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})